﻿@using Telerik.Web.Mvc.UI;
@using System.Linq;
@{
    var gridPageSize = EngineContext.Current.Resolve<SmartStore.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    
    ViewBag.Title = T("Admin.Catalog.ProductTags").Text;
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "product-tags-form" }))
{
    <div class="section-header">
        <div class="title">
            <i class="fa fa-tags"></i>
            @T("Admin.Catalog.ProductTags")
        </div>
    </div>

    <div>
		@(Html.Telerik().Grid<ProductTagModel>()
			.Name("product-tags-grid")
			.DataKeys(x => { x.Add(y => y.Id).RouteKey("Id"); })
			.Columns(columns =>
			{
				columns.Bound(x => x.Name);
				columns.Bound(x => x.ProductCount)
					.Centered()
					.Width(150);
				columns.Command(commands =>
				{
					commands.Custom("edit-product-tag").Text(T("Common.Edit"));
					commands.Delete().Localize(T);
				})
				.Width(90)
				.HtmlAttributes(new { align = "right" });
			})
			.DataBinding(dataBinding =>
			{
				dataBinding.Ajax()
					.Select("ProductTags", "Product")
					.Delete("ProductTagDelete", "Product");
			})
			.Filterable()
			.Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
			.PreserveGridState()
			.EnableCustomBinding(true))

		<input type="submit" id="btnRefresh" name="btnRefresh" style="display: none" />
	</div>
}

<script type="text/javascript">
	$(document).ready(function () {
		$('#btnRefresh').click(function () {
			//refresh grid
			var optionsGrid = $("#product-tags-grid");
			optionsGrid.data('tGrid').ajaxRequest();

			//return false to don't reload a page
			return false;
		});

		$('#product-tags-grid').on('click', '.t-grid-edit-product-tag', function (e) {
			e.preventDefault();
			var grid = $('#product-tags-grid').data('tGrid');
			var tr = $(this).closest('tr');
			var id = grid.dataItem(tr).Id;
			var href = "@Url.Content("~/Admin/Product/EditProductTag/")" + id + "?btnId=btnRefresh&formId=product-tags-form";

			openPopup(href);

			return false;
		});
	});
</script>